package com.hex.ds.hdrs.warn.service;

import com.hex.bigdata.hbda.utils.StringUtils;
import com.hex.ds.hdrs.common.constant.Const;
import com.hex.ds.hdrs.common.utils.BaseUtil;
import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import com.hex.ds.hdrs.warn.dao.WarnSendMethodDao;
import com.hex.ds.hdrs.warn.dto.WarnSendMethodDto;
import com.hex.ds.hdrs.warn.po.WarnContent;
import com.hex.ds.hdrs.warn.po.WarnSendMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @Package: com.hex.ds.hdrs.warn.service
 * @ClassName WarnSendMethodService
 * @Description:
 * @Author: ji.tang
 * @Date 2022/4/27 10:42
 * @Version v1.0
 **/
@Slf4j
@Service
public class WarnSendMethodService {

    @Autowired
    private WarnSendMethodDao warnSendMethodDao;

    @Autowired
    private WarnContentService warnContentService;

    /**
     * @Method: add
     * @Param: [noticInfMap, subject, content, jobCode]
     * @Return: void
     * @Description: 新增告警发送内容和方式
     * @Author: ji.tang
     * @Date: 2022/4/30 15:53
     * @Version: V1.0
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(Map noticInfMap, String subject, String content, String jobCode,String confId) {
        try {
            String contentId = BaseUtil.getPkId();
            WarnContent warnContent = new WarnContent(contentId,jobCode, HdrsDateUtil.getCurrentTime(), HdrsDateUtil.getCurrentTime(),content,confId);

            //新增发送内容
            this.warnContentService.insert(warnContent);
            if (!CollectionUtils.isEmpty(noticInfMap)) {
                Iterator it = noticInfMap.keySet().iterator();
                while (it.hasNext()) {
                    String key;
                    key = it.next().toString();
                    List<String> list = (List<String>) noticInfMap.get(key);
                    WarnSendMethod sendMethod = new WarnSendMethod(BaseUtil.getPkId(),key,Const.WARN_SEND_STATUS_NO,
                            StringUtils.join(list, ","), HdrsDateUtil.getCurrentTime(), HdrsDateUtil.getCurrentTime(),contentId);
                    if (key.equals(Const.WARN_TYPE_MAIL)) {
                        //是否html
                        sendMethod.setIsHtml((false) ? Const.MAIL_IS_HTML : Const.MAIL_IS_TXT);
                        //主题
                        sendMethod.setSubject(subject);
                    }
                    //新增发送方式
                    this.insert(sendMethod);
                }
            }
        } catch (Exception e) {
            log.error("告警内容或者告警方式新增异常：{}", e.getMessage(), e);

        }
    }

    /**
     * @Method: insert
     * @Param: [warnSendMethod]
     * @Return: void
     * @Description:
     * @Author: ji.tang
     * @Date: 2022/4/27 11:04
     * @Version: V1.0
     */
    public void insert(WarnSendMethod warnSendMethod) {
        this.warnSendMethodDao.insert(warnSendMethod);
    }

    /**
     * @Method: selectWarnList
     * @Param: [map]
     * @Return: java.util.List<com.hex.ds.hdrs.warn.dto.WarnSendMethodDto>
     * @Description: 查询预警发送信息
     * @Author: ji.tang
     * @Date: 2022/4/27 15:51
     * @Version: V1.0
     */
    public List<WarnSendMethodDto> selectWarnList(Map map) {
        List<WarnSendMethodDto> methodDtos = new ArrayList<>();
        if (CollectionUtils.isEmpty(map)) {
            return null;
        } else {
            List<WarnSendMethod> list = this.warnSendMethodDao.selectAll(map);
            if (!CollectionUtils.isEmpty(list)) {
                for (WarnSendMethod s : list) {
                    WarnContent warnContent = this.warnContentService.selectById(s.getWarnConId());
                    if (null != warnContent) {
                        WarnSendMethodDto dto = new WarnSendMethodDto();
                        dto = this.cloneObj(s);
                        dto.setContent(warnContent.getWarnContent());
                        methodDtos.add(dto);
                    }
                }
            }
        }
        return methodDtos;
    }

    /**
     * @Method: edit
     * @Param: [warnSendMethod]
     * @Return: void
     * @Description: 修改预警发送方式
     * @Author: ji.tang
     * @Date: 2022/5/20 17:33
     * @Version: V1.0
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void edit(WarnSendMethod warnSendMethod) {
        this.warnSendMethodDao.edit(warnSendMethod);
    }

    private WarnSendMethodDto cloneObj(WarnSendMethod mt) {
        WarnSendMethodDto dto = new WarnSendMethodDto();
        dto.setPkId(mt.getPkId());
        dto.setAttachmentPath(mt.getAttachmentPath());
        dto.setBccAddr(mt.getBccAddr());
        dto.setCcAddr(mt.getCcAddr());
        dto.setCrtTs(mt.getCrtTs());
        dto.setCrtUser(mt.getCrtUser());
        dto.setIsHtml(mt.getIsHtml());
        dto.setSendStatus(mt.getSendStatus());
        dto.setSendTime(mt.getSendTime());
        dto.setSubject(mt.getSubject());
        dto.setToAddr(mt.getToAddr());
        dto.setUpTs(mt.getUpTs());
        dto.setUpUser(mt.getUpUser());
        dto.setWarnConId(mt.getWarnConId());
        dto.setWarnType(mt.getWarnType());
        return dto;
    }
}
